package com.zoe.onelink.business.service;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zoe.onelink.business.dao.CommonDao;
import com.zoe.onelink.common.enumeration.ExceptionCodeEnum;
import com.zoe.onelink.common.exception.ApiException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

/**
 * <p>标题: 弃用</p>
 * <p>描述: </p>
 * <p>版权: Copyright (c) 2017</p>
 * <p>公司: 智业软件股份有限公司</p>
 *
 * @version: 1.0
 * @author: my36z
 * @date 2017/10/25
 */
@Service
@Deprecated
public class CommonService {

    @Autowired
    CommonDao commonDao;

    @Value("${sql.maxRow:1000}")
    private Integer maxRow;

    static String reg = "(?:--)"
            + "(\\b(insert|delete|update|create|drop|truncate|alter|grant|execute|exec|xp_cmdshell|call|declare|source)\\b)";

    static Pattern sqlPattern = Pattern.compile(reg, Pattern.CASE_INSENSITIVE);

    /**
     * 查询SQL结果，对SQL进行删除验证，避免执行删除语句，对返回条数限制，避免返回过多的数据
     *
     * @param sql
     * @return
     * @throws Exception
     */
    public List<Map<String, Object>> findAll(String sql) throws Exception {
        if (sqlPattern.matcher(sql).find()) {
            throw ApiException.createEx(ExceptionCodeEnum.DB_SQL_ERROR, "SQL异常");
        }
        return commonDao.findAll(sql, Page.of(1, this.maxRow));
    }

}
